package com.ylsdxy.school_sys_set.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylsdxy.school_sys_set.service.*;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ylsdxy.model.entity.*;
import org.ylsdxy.model.response.ResponseResult;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/schoolSet")
public class SchoolController {
    @Resource
    CampusService campusService;

    @Resource
    SubjectService subjectService;

    @Resource
    RankService rankService;

    @Resource
    ClassRoomService classRoomService;
    @Resource
    SetMealService setMealService;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    SchoolService schoolService;

    @Resource
    CampusRoomService campusRoomService;


    //校区信息
    @RequestMapping("/findCampus")
    public ResponseResult<Page<Campus>> getCampus(){
        QueryWrapper<Campus> campusQueryWrapper = new QueryWrapper<>();
        campusQueryWrapper.eq("is_delete",0);
        Page<Campus> campusList=  campusService.page(new Page<>(1,10),campusQueryWrapper);
        redisTemplate.opsForValue().set("campusList",campusList);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",campusList);
    }
    //校区删除

    @RequestMapping("/delCampus/{id}")
    public ResponseResult<Boolean> delCampus( @PathVariable("id") Integer id){
       Boolean campusList=  campusService.removeById(id);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",campusList);
    }
    //校区修改
    @RequestMapping("/updateCampus")
    public ResponseResult<Boolean> delCampus( @RequestBody Campus campus){
        Boolean campusList=  campusService.updateById(campus);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",campusList);
    }
    //校区添加
//    @PreAuthorize("hasAnyAuthority('schoolSet.add')")
    @RequestMapping("/insertCampus")
    public ResponseResult<Boolean> insertCampus( @RequestBody Campus campus){
         campus.setCreatTime(new Date());
        Boolean s= campusService.save(campus);
        if (s) {
            return new ResponseResult<>(ResponseResult.SUCCESS, "success", s);
        }else {
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR, "error", s);
        }
    }

    //查询科目
    @RequestMapping("/findSubject")
    public ResponseResult<Page<Subject>> getSubject(){
        QueryWrapper<Subject> campusQueryWrapper = new QueryWrapper<>();
        campusQueryWrapper.eq("is_delete",0);
        Page<Subject> subjectList=  subjectService.page(new Page<>(1,10),campusQueryWrapper);
        redisTemplate.opsForValue().set("subjectList",subjectList);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",subjectList);
    }
    //科目删除
    @RequestMapping("/delSubject/{id}")
    public ResponseResult<Boolean> delSubject( @PathVariable("id") Integer id){
        Boolean subjectList=  subjectService.removeById(id);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",subjectList);
    }
    //科目修改
    @RequestMapping("/updateSubject")
    public ResponseResult<Boolean> delSubject( @RequestBody Subject subject){
        Boolean subjectList=  subjectService.updateById(subject);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",subjectList);
    }
    //科目添加
    @RequestMapping("/insertSubject")
    public ResponseResult<Boolean> insertSubject( @RequestBody Subject subject){
        subject.setCreatTime(new Date());
        Boolean s= subjectService.save(subject);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",s);
    }

    //查询年级
    @RequestMapping("/findRank")
    public ResponseResult<Page<RankTable>> getRank(){
        QueryWrapper<RankTable> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        Page<RankTable> rankList= rankService.page(new Page<>(1,10),wrapper);
        redisTemplate.opsForValue().set("rankList",rankList);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",rankList);
    }
    //  年级删除
    @RequestMapping("/delRank/{id}")
    public ResponseResult<Boolean> delRank( @PathVariable("id") Integer id){
        Boolean s=  rankService.removeById(id);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",s);
    }
    //年级修改
    @RequestMapping("/updateRank")
    public ResponseResult<Boolean> updateRank( @RequestBody RankTable rank){
        Boolean s=  rankService.updateById(rank);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",s);
    }
    //年级添加
    @RequestMapping("/insertRank")
    public ResponseResult<Boolean> insertRank( @RequestBody RankTable rank){
        Boolean s= rankService.save(rank);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",s);
    }
    //查询教室
    @RequestMapping("/findClassRoom")
    public ResponseResult<Page<SchoolRoom>> getClassRoom(){

        QueryWrapper<SchoolRoom> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        Page<SchoolRoom> classRoomList= classRoomService.page(new Page<>(1,10),wrapper);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",classRoomList);
    }

    @RequestMapping("/findClassRoom1/{id}")
    public ResponseResult<List<SchoolRoom>> getClassRoom1( @PathVariable("id") Integer  id){
        QueryWrapper<CampusRoom> wrapper1 = new QueryWrapper<>();
           wrapper1.eq("campus_id",id);
        List<CampusRoom> campusRoomList = campusRoomService.list(wrapper1);
        if (!campusRoomList.isEmpty()) {
            List<Integer> collect = campusRoomList.stream().map(CampusRoom::getRoomId).collect(Collectors.toList());
            List<SchoolRoom> schoolRooms = classRoomService.listByIds(collect);
            List<SchoolRoom> collect1 = schoolRooms.stream().filter(schoolRoom -> schoolRoom.getIsDelete()==0).collect(Collectors.toList());
            return new ResponseResult<>(ResponseResult.SUCCESS, "success", collect1);
        }
        return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"no data",null);
    }
    //  教室删除
    @RequestMapping("/delClassRoom/{id}/{cid}")
    public ResponseResult<Boolean> delClassRoom( @PathVariable("id") Integer id,@PathVariable("cid") Integer cid){
        if(cid==null) {
            Boolean s = classRoomService.removeById(id);
            return new ResponseResult<>(ResponseResult.SUCCESS, "success", s);
        }else {
            QueryWrapper<CampusRoom> wrapper = new QueryWrapper<>();
            wrapper.eq("campus_id",cid)
                    .eq("room_id",id);
            boolean remove = campusRoomService.remove(wrapper);
            return new ResponseResult<>(ResponseResult.SUCCESS, "success", remove);
        }
    }
    //教室修改
    @RequestMapping("/updateClassRoom")
    public ResponseResult<Boolean> updateClassRoom( @RequestBody SchoolRoom classRoom){
        Boolean s=  classRoomService.updateById(classRoom);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",s);
    }
    //教室添加
    @RequestMapping("/insertClassRoom/{cid}")
    public ResponseResult<Boolean> insertClassRoom( @RequestBody SchoolRoom classRoom,@PathVariable("cid") Integer cid){
        classRoom.setCreateTime(new Date());
        Boolean s= classRoomService.save(classRoom);
        if (cid!=null){
            CampusRoom campusRoom = new CampusRoom();
            campusRoom.setCampusId(cid);
            QueryWrapper<SchoolRoom> wrapper = new QueryWrapper<>();
            wrapper.eq("class_name",classRoom.getClassName());
            campusRoom.setRoomId(classRoomService.getOne(wrapper).getId());
            boolean save = campusRoomService.save(campusRoom);
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",save);
        }
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",s);
    }

    //查询套餐
    @RequestMapping("/findSetMeal")
    public ResponseResult<Page<SetMeal>> getSetMeal(){
        QueryWrapper<SetMeal> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        Page<SetMeal> setMealList= setMealService.page(new Page<>(1,10),wrapper);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",setMealList);
    }
    //  删除套餐
    @RequestMapping("/delSetMeal/{id}")
    public ResponseResult<Boolean> delSetMeal( @PathVariable("id") Integer id){
        Boolean s=  setMealService.removeById(id);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",s);
    }
    //修改套餐
    @RequestMapping("/updateSetMeal")
    public ResponseResult<Boolean> updateSetMeal( @RequestBody SetMeal SetMeal){
        Boolean s=  setMealService.updateById(SetMeal);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",s);
    }
    //添加套餐
    @RequestMapping("/insertSetMeal")
    public ResponseResult<Boolean> insertSetMeal( @RequestBody SetMeal SetMeal){
        Boolean s= setMealService.save(SetMeal);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",s);
    }

    //查询学校信息
    @RequestMapping("/findSchool")
    public ResponseResult<School> getSchool(){
        School one = schoolService.getOne(null);
        return  new ResponseResult<>(ResponseResult.SUCCESS,"success",one);
    }
    @RequestMapping("/updateSchool")
    public ResponseResult<Boolean> updateSchool(@RequestBody School school){
        Boolean s = schoolService.update(school,null);
        return  new ResponseResult<>(ResponseResult.SUCCESS,"success",s);
    }

}
